/*
 * Copyright (c) 2015 DHC Corporation.
 */

package cn.com.dhc.danlu.activity.category;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import java.util.Timer;
import java.util.TimerTask;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.BaseActivity;
import cn.com.dhc.danlu.util.CommonConst;
import cn.com.dhc.danlu.util.CommonUtils;

/**
 * 商品关键字搜索画面
 *
 * @author LiYongHe
 * @version 1.00 2015/03/17 新建
 */
public class GoodInfoSearchActivity extends BaseActivity {
    /**
     * 确定按钮
     */
    private LinearLayout llytSearch;
    /**
     * 搜索文本框
     */
    private EditText edtSearch;
    OnEditorActionListener listener = new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
            if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
                Intent intent = new Intent(GoodInfoSearchActivity.this, CategoryActivity.class);
                intent.putExtra(CommonConst.IntentKey.KEY_WORD, edtSearch.getText().toString());
                // 设置分类页面回调参数
                setResult(RESULT_OK, intent);
                // 隐藏软键盘
                CommonUtils.hideSoftInputForce(GoodInfoSearchActivity.this);
                // 清除缓存
                CommonUtils.clearAllCgrPrtBrd(getApplicationContext());
                finish();
                return true;
            }
            return false;
        }
    };
    private LinearLayout llytBack;
    /**
     * 搜索您喜欢的饮品背景
     */
    private RelativeLayout rlySearchBG;
    /**
     * 判断edit是否有输入
     */
    private TextWatcher edtWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            rlySearchBG.setVisibility(View.GONE);
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            if (arg0.toString().equals("")) {
                rlySearchBG.setVisibility(View.GONE);
            } else {
                rlySearchBG.setVisibility(View.VISIBLE);
            }
        }
    };
    /**
     * 新建一个监听点击事件对象
     */
    private OnClickListener goodSearchOnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.llytBack:
                    finish();
                    break;

                // 如果是点击确定按钮
                case R.id.llytSearch:
                    // 新建意图对象
                    Intent intent = new Intent(GoodInfoSearchActivity.this, CategoryActivity.class);
                    intent.putExtra(CommonConst.IntentKey.KEY_WORD, edtSearch.getText().toString());
                    // 设置分类页面回调参数
                    setResult(RESULT_OK, intent);
                    // 隐藏软键盘
                    CommonUtils.hideSoftInputForce(GoodInfoSearchActivity.this);
                    // 清除缓存
                    CommonUtils.clearAllCgrPrtBrd(getApplicationContext());
                    finish();
                    break;

                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getDelegate().setContentView(R.layout.layout_good_info_search, R.layout_good_info_search.class);
        setContentView(R.layout.layout_good_info_search);

        initViews();
        setViews();
        setLisetListener();
    }

    private void initViews() {
        llytBack = (LinearLayout) findViewById(R.id.llytBack);
        llytSearch = (LinearLayout) findViewById(R.id.llytSearch);
        edtSearch = (EditText) findViewById(R.id.edtSearch);
        rlySearchBG = (RelativeLayout) findViewById(R.id.rlySearchBG);
    }

    @Override
    protected String getActivityTitle() {
        return null;
    }

    /**
     * 画面初始化
     */
    private void setViews() {
        // 搜索本地缓存，显示历史搜索
        String search = getIntent().getStringExtra("search");
        if (search != null && !search.equals("搜索")) {
            edtSearch.setText(search);
        }
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            public void run() {
                InputMethodManager inputManager = (InputMethodManager) edtSearch.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(edtSearch, 0);
            }

        }, 500);
        edtSearch.setSelection(edtSearch.getText().length());
    }

    /**
     * 设置按钮的监听方法
     */
    private void setLisetListener() {
        edtSearch.addTextChangedListener(edtWatcher);
        llytSearch.setOnClickListener(goodSearchOnClickListener);
        edtSearch.setOnEditorActionListener(listener);
    }

    @Override
    protected void onResume() {
        super.onResume();
        edtSearch.requestFocus();
    }
}